<?php
class ControllerAddonSale extends Controller
{
	private $error = array();
	
	public function index()
	{
		$this->load->language('addon/order');
		$this->data = array_merge($this->data, $this->language->getData());
		$type = $this->request->get['type'];
		
		$this->data['heading_title'] = "Sale Order List";
		$this->load->model("addon/order");
		$orderid = $this->request->get['orderid'];
		if($orderid=="")
			$this->getList();
		else
			$this->getForm();
	}
	
	public function view()
	{
		$this->data['heading_title'] = "Sale Order List";
		$orderid = $this->request->get['orderid'];
		$this->load->model("addon/order");
		$this->load->model("core/user");
		$this->load->helper('image');
		$this->data = $this->model_addon_order->getItem($orderid);
		if($this->data['order']['status']=='')
			$this->data['order']['text_active'] = "Pending";
		else
			$this->data['order']['text_active'] = "Completed";
		/*foreach($this->data['detail'] as $key => $item)
		{
			/*$imagepreview = "<img width=100 src='".HelperImage::resizePNG($item['imagepath'], 180, 180)."' >";
			$this->data['detail'][$key]['imagepreview'] = $imagepreview;*/
			
		//}*/
		$this->data['order']['orderdate'] = $this->date->formatMySQLDate($this->data['order']['orderdate']);
		$this->data['order']['age'] = $this->model_addon_order->getInforOrderValue($orderid,"age");
		$this->data['order']['gender'] = $this->model_addon_order->getInforOrderValue($orderid,"gender");
		$user = $this->model_core_user->getItem($this->data['order']['doctor']);
		$this->data['order']['doctor']=$user['fullname'];
		$this->data = array_merge($this->data, $this->language->getData());
		
		$this->id='content';
		$this->template="addon/sale_view.tpl";
		//$this->layout="layout/center";
		$this->render();
	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('addon/sale/insert');
		$this->data['delete'] = $this->url->http('addon/sale/delete');		

		$this->data['orders'] = array();
		$where ="";
		$data=$this->request->get;
		if($data['productname']!="")
		{
			$w = " AND mediaid in (Select mediaid from media Where title like '%".$data['productname']."%') AND type ='sale'";
			$list = $this->model_addon_order->getListOrderProduct($w);
			$arr = $this->string->matrixToArray($list,"orderid");
			$where .= " AND orderid in('".implode("','",$arr)."')";
		}
		if($data['orderidfiter']!="")
			$where .= " AND orderid like '%".$data['orderidfiter']."%'";
		if($data['createby']!="")
			$where .= " AND createby like '%".$data['createby']."%'";
		if($data['customername']!="")
			$where .= " AND customername like '%".$data['customername']."%'";
		if($data['startdate']!="")
			$where .= " AND orderdate >='".$this->date->formatViewDate($data['startdate'])."'";
		if($data['enddate']!="")
			$where .= " AND orderdate <='".$this->date->formatViewDate($data['enddate'])."'";
		if($data['insuranceid']!="")
			$where .= " AND insuranceid like '%".$data['insuranceid']."%'";
		if($data['insurancepercent']!="")
			$where .= " AND insurancepercent ='".$data['insurancepercent']."'";
		$where .= " AND `type` = 'sale' ORDER BY  `order`.`orderdate` DESC ";
		$rows = $this->model_addon_order->getListOrder($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		{
			$this->data['orders'][$i] = $rows[$i];
			$this->data['orders'][$i]['link_active'] = $this->url->http('addon/order/check&orderid='.$this->data['orders'][$i]['orderid']);
			if($this->data['orders'][$i]['status']=='')
				$this->data['orders'][$i]['text_active'] = "Pending";
			else
				$this->data['orders'][$i]['text_active'] = "Completed";
		}
		$this->id='content';
		$this->template="addon/sale_list.tpl";
		$this->layout="layout/center";
		$this->render();
	}
	
	public function updatestatus()
	{
		$this->load->model("addon/order");
		$orderid = $this->request->get['orderid'];
		//$this->data = $this->model_addon_order->getItem($orderid);
		$data['orderid'] = $orderid;
		$data['status'] = "checked";
		$this->model_addon_order->updateStatus($data);
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function insert()
	{
		$this->getForm();
	}
	
	private function getForm()
	{
		$this->load->model("addon/order");
		$this->load->model("core/sitemap");
		$this->load->model("core/media");
		$this->data['order']['orderdate'] = $this->date->formatMySQLDate($this->date->getToday());
		$orderid = $this->request->get['orderid'];
		
		if($orderid)
		{
			$this->data =array();
			$this->data=$this->model_addon_order->getItem($orderid);
			$this->data['order']['orderdate'] = $this->date->formatMySQLDate($this->data['order']['orderdate']);
			$this->data['order']['age'] = $this->model_addon_order->getInforOrderValue($orderid,"age");
			$this->data['order']['gender'] = $this->model_addon_order->getInforOrderValue($orderid,"gender");
		}
		$this->data['treecategory'] = array();
		$this->model_core_sitemap->getTree("category",$this->data['treecategory']);
		$this->data['treeservice'] = array();
		$this->model_core_sitemap->getTree("service",$this->data['treeservice']);
		$this->data['medias'] = $this->model_core_media->getList();
		$this->id='content';
		$this->template="addon/sale_form.tpl";
		$this->layout="layout/center";
		$this->render();
	}
	
	public function savepost()
	{
		$data = $this->request->post;
		$this->data['errors'] = $this->validate($data);
		if(count($this->data['errors'])==0)
		{
			$data['orderdate'] = $this->date->formatViewDate($data['orderdate']);
			$data['type'] = 'sale';
			$this->load->model("addon/order");
			$orderid="";
			if($data['orderid']=="")
				$orderid = $this->model_addon_order->insert($data);
			else
				$orderid = $this->model_addon_order->update($data);
			//Save information	
			
			/*$this->model_addon_order->saveInforOrder($orderid,"age",$data['age']);
			$this->model_addon_order->saveInforOrder($orderid,"gender",$data['gender']);*/
			//Remove order detail
			if(count($data['remove']))
				foreach($data['remove'] as $item)
				{
					$this->model_addon_order->removeOrderProduct($item);	
				}
			
			//Save Orderdetail
			foreach($data['product'] as $key => $item)
			{
				$dataorder['id'] = $data['id'][$key];
				$dataorder['orderid'] = $orderid;
				$dataorder['mediaid'] = $item;
				$dataorder['quantity'] = str_replace("," ,"",$data['quantity'][$key]);
				$dataorder['price'] = str_replace("," ,"",$data['price'][$key]);
				$dataorder['discount'] = str_replace("," ,"",$data['discount'][$key]);
				$dataorder['subtotal'] = $dataorder['quantity']*$dataorder['price'];
				$dataorder['type'] = $data['type'].$data['service'][$key];
				$this->model_addon_order->saveOrderProduct($dataorder);	
			}
			$this->model_addon_order->updateTotal($orderid);
			$this->data['output'] = $orderid;			
		}
		else
			$this->data['output'] = "false";
			
		$this->id='import';
		$this->template='common/output.tpl';	
		$this->render();
	}
	
	function validate($data)
	{
		$err = array();
		if($data['createby']=="")
			$err['createby'] ="Create by not null";
		if($data['customername']=="")
			$err['customername'] ="Customer name not null";
		if($data['orderdate']=="")
			$err['orderdate'] ="Date by not null";
		return $err;
	}
}
?>